import dayjs from 'dayjs';
import * as Types from './type';
import * as ApiTypes from '@/api/project/project.d';
import {ref} from 'vue'

/**
 * 把视图层表单转换成接口表单数据结构(给接口用)
 * @param form 视图层表单
 * @param apiForm 接口层表单
 */
export const transformForm = (
  form: Types.ProjectDetailViewForm,
  apiForm: Types.ProjectDetailStateForm,
  lesionList: ApiTypes.LesionConfigItem[]
) => {
  formatForm(form.basicSetting, apiForm.basicSetting);
  formatForm(form.annoSetting, apiForm.annoSetting);
  formatForm(form.dataSetting, apiForm.dataSetting);
  formatForm(form.annoUserSetting, apiForm.annoUserSetting);
  console.log(form, 'form');
  console.log(apiForm, 'apiForm');
};

/**
 * 将最外层对象循环处理表单并将需要转的对象字段进行转换
 * @param form 视图层表单
 * @param apiForm 接口层表单
 */
export const formatForm = (
  form:
    | Types.ViewBasicSetting
    | Types.ViewAnnoSetting
    | Types.ViewDataSetting
    | Types.ViewAnnoUserSetting,
  apiForm:
    | Types.BasicSetting
    | Types.AnnoSetting
    | Types.DataSetting
    | Types.AnnoUserSetting,
  lesionList?: ApiTypes.LesionConfigItem[]
) => {
  for (const key in form) {
    if (key === 'discardCode' && Array.isArray(form[key])) {
      apiForm[key] = form[key].join(',');
    }else if (key === 'seriesFcList' && Array.isArray(form[key])) {
      apiForm[key] = form['seriesDataList'].map((item) => ({
        fcId: item.id,
        required: item.required,
      }));
    }else if (key === 'lesionList' && Array.isArray(form[key])) {
      form['lesionDataList'] = form['lesionDataList'].map((item) => {
        item.imageFcList = item.imageDataList.map((t) => ({
          fcId: t.id,
          required: t.required,
        }));
        return item;
      });
      apiForm[key] = form['lesionDataList'].map((item) => {
        const { name, imageList, imageDataList, ...newItem } = item;
        return newItem;
      });
    }else if (key === 'seriesList') {
      apiForm['annoSeriesList'] = form[key]
      delete apiForm['seriesList']
    }else if (key === 'beginDate' || key === 'endDate') {
      apiForm[key] = dayjs(form[key]).valueOf();
    }else if (key === 'annoUserList' && Array.isArray(form[key])) {
      apiForm[key] = form[key].map((item) => {
        const { userDataList, ...newItem } = item;
        return newItem;
      });
    }else{
      apiForm[key] = form[key];
    }
    delete apiForm['seriesDataList'];
    delete apiForm['lesionDataList'];
    delete apiForm['totalUserNum'];
    delete apiForm['createTime'];
    delete apiForm['createUser'];
  }
};

/**
 * 把接口数据转换成视图层表单数据结构(给渲染用)
 * @param form 视图层表单
 * @param apiForm 接口层表单
 */
export const transformApiForm = (
  form: Types.ProjectDetailViewForm,
  apiForm: Types.ProjectDetailStateForm,
  lesionList: ApiTypes.LesionConfigItem[],
  getCompDialog?: Function
) => {
  formatApiForm(form.basicSetting, apiForm.basicSetting);
  formatApiForm(
    form.annoSetting,
    apiForm.annoSetting,
    lesionList,
    getCompDialog
  );
  formatApiForm(form.dataSetting, apiForm.dataSetting);
  formatApiForm(form.annoUserSetting, apiForm.annoUserSetting);
  return ref(form).value
  console.log(form, 'form');
  console.log(apiForm, 'apiForm');
};

/**
 * 将最外层对象循环处理表单并将需要转的对象字段进行转换
 * @param form 视图层表单
 * @param apiForm 接口层表单
 */
export const formatApiForm = (
  form:
    | Types.ViewBasicSetting
    | Types.ViewAnnoSetting
    | Types.ViewDataSetting
    | Types.ViewAnnoUserSetting,
  apiForm:
    | Types.BasicSetting
    | Types.AnnoSetting
    | Types.DataSetting
    | Types.AnnoUserSetting,
  lesionList?: ApiTypes.LesionConfigItem[],
  getCompDialog?: Function
) => {
  for (const key in apiForm) {
    if (key === 'discardCode') {
      apiForm[key] = apiForm[key]?? ''
      form[key] = apiForm[key].split(',');
    }else if (key === 'seriesFcList') {
      apiForm[key] = apiForm[key]?? []
      form[key] = apiForm[key].map((item) => item.fcId);
      getCompDialog && getCompDialog(apiForm[key], true);
    }else if (key === 'lesionList') {
      lesionList = lesionList??[]
      const tempArr = [] as ApiTypes.LesionConfigItem[];
      lesionList?.map((item) => {
        if (item.childs && item.childs.length !== 0) {
          item.childs.map((t) => {
            tempArr.push(t);
          });
        } else {
          tempArr.push(item);
        }
      });
      form['lesionDataList'] = apiForm[key]?.map((item) => {
        const imageList = item.imageFcList.map((t) => t.fcId);
        const imageDataList = [];
        getCompDialog && getCompDialog(item.imageFcList, false, imageDataList);
        return {
          ...item,
          imageList,
          imageDataList,
          name: tempArr?.find((t) => t.value === item.lesionValue)?.name,
          // bizType: bizType ?? bizType + '',
        };
      });
      // form['lesionDataList'] = form['lesionDataList'].map((item) => {
      //   const imageList = item.imageFcList.map((t) => t.fcId);
      //   getCompDialog && getCompDialog(imageList, false, item.lesionValue);
      //   item.imageDataList = item.imageDataList.map((it) => {
      //     item.imageFcList.map((t) => {
      //       if (it.id === t.fcId) {
      //         it.required = t.required;
      //       }
      //     });
      //   });
      // });
      form[key] = apiForm[key]?.map((item) => item.lesionValue);
    }else if (key === 'annoUserList') {
      apiForm[key] = apiForm[key] ?? [];
      form[key] = apiForm[key].map((item)=>{
        let it:any = {}
        it.companyId = item.company.id
        it.userIdList = item.userList.map(pt => pt.id)
        it.userList = item.userList
        return it
      })
    }else if(key === 'seriesList'){
      apiForm[key] = apiForm[key] ?? [];
      form[key] = apiForm[key].map((it)=>{
        let item:any = {...it}
        if(item.refResultTaskId === null){
          item.refResultTaskId = -1
        }
        return item
      });
    }else{
      form[key] = apiForm[key]
    }
  }
};

/**
 * 根据病灶选择实时变更表格数据
 * @param lesions 表单标注类型选择值
 * @param tableData 表格渲染数据
 */
export const getFormLeisonDataList = (
  lesions: string,
  tableData: ApiTypes.LesionItem[]
) => {};
